package de.tempel.pong.objects;

import processing.core.PApplet;
import de.tempel.pong.objects.interfaces.Drawable;

/**
 * 
 * @author christina
 * 
 */
public class ScoreBoard implements Drawable {

	private int points = 0;
	private final int x; // x position
	private final int digitWidth;
	private final int digitHeight;
	private final int digitDistance;
	private final int lineWidth;
	private final int distanceFromTop;

	private PApplet window;

	/**
	 * 
	 * @param x
	 * @param distanceFromTop
	 * @param height
	 * @param digitWidth
	 * @param digitDistance
	 */
	public ScoreBoard(int x, int distanceFromTop, int height, int digitWidth,
			int digitDistance) {
		this.x = x;
		this.digitHeight = height;
		this.digitWidth = digitWidth;
		this.digitDistance = digitDistance;
		this.lineWidth = this.digitWidth / 3;
		this.distanceFromTop = distanceFromTop;
	}

	@Override
	public void draw(PApplet window) {
		this.window = window;
		draw();
	}

	public void addPoint() {
		this.points++;
	}

	public int getPoints() {
		return points;
	}

	public void setPoints(int points) {
		this.points = points;
	}

	private void draw() {
		String points = Integer.toString(this.points);
		char[] digits = points.toCharArray();
		if (this.points > 999) {
			drawUnits(9);
			drawTens(9);
			drawHundreds(9);
		} else if (this.points < 10) {
			drawUnits(Character.getNumericValue(digits[0]));
			drawTens(0);
			drawHundreds(0);
		} else if (this.points < 100) {
			drawUnits(Character.getNumericValue(digits[1]));
			drawTens(Character.getNumericValue(digits[0]));
			drawHundreds(0);
		} else {
			drawUnits(Character.getNumericValue(digits[2]));
			drawTens(Character.getNumericValue(digits[1]));
			drawHundreds(Character.getNumericValue(digits[0]));
		}
		this.window.fill(255);
		this.window.stroke(0);
	}

	private void drawHundreds(int c) {
		drawDigit(c, this.x);
	}

	private void drawTens(int c) {
		drawDigit(c, this.x + this.digitWidth + this.digitDistance);

	}

	private void drawUnits(int c) {
		drawDigit(c, this.x + 2 * this.digitDistance + 2 * this.digitWidth);

	}

	private void drawDigit(int c, int x) {
		switch (c) {
		case (0):
			window.fill(255);
			window.rect(x, this.distanceFromTop, this.digitWidth,
					this.digitHeight);
			window.fill(0);
			window.rect(x + this.lineWidth, this.distanceFromTop
					+ this.lineWidth, this.lineWidth, this.digitHeight - 2
					* this.lineWidth);
			break;
		case (1):
			window.fill(255);
			window.rect(x + this.digitWidth - this.lineWidth,
					this.distanceFromTop, this.lineWidth, this.digitHeight);
			break;
		case (2):
			window.fill(255);
			window.rect(x, this.distanceFromTop, this.digitWidth,
					this.digitHeight);
			window.fill(0);
			window.rect(x, this.distanceFromTop + this.lineWidth,
					2 * this.lineWidth, this.lineWidth);
			window.rect(x + this.lineWidth, this.distanceFromTop + 3
					* this.lineWidth, 2 * this.lineWidth, this.lineWidth);
			break;
		case (3):
			window.fill(255);
			window.rect(x, this.distanceFromTop, this.digitWidth,
					this.digitHeight);
			window.fill(0);
			window.rect(x, this.distanceFromTop + this.lineWidth,
					2 * this.lineWidth, this.lineWidth);
			window.rect(x, this.distanceFromTop + 3 * this.lineWidth,
					2 * this.lineWidth, this.lineWidth);
			break;
		case (4):
			window.fill(255);
			window.rect(x, this.distanceFromTop, this.digitWidth,
					this.digitHeight);
			window.fill(0);
			window.rect(x + this.lineWidth, this.distanceFromTop,
					this.lineWidth, this.digitHeight - 3 * this.lineWidth);
			window.rect(x, this.distanceFromTop + 3 * this.lineWidth,
					2 * this.lineWidth, 2 * this.lineWidth);
			break;
		case (5):
			window.fill(255);
			window.rect(x, this.distanceFromTop, this.digitWidth,
					this.digitHeight);
			window.fill(0);
			window.rect(x + this.lineWidth, this.distanceFromTop
					+ this.lineWidth, 2 * this.lineWidth, this.lineWidth);
			window.rect(x, this.distanceFromTop + 3 * this.lineWidth,
					2 * this.lineWidth, this.lineWidth);
			break;
		case (6):
			window.fill(255);
			window.rect(x, this.distanceFromTop, this.digitWidth,
					this.digitHeight);
			window.fill(0);
			window.rect(x + this.lineWidth, this.distanceFromTop
					+ this.lineWidth, 2 * this.lineWidth, this.lineWidth);
			window.rect(x + this.lineWidth, this.distanceFromTop + 3
					* this.lineWidth, this.lineWidth, this.lineWidth);
			break;
		case (7):
			window.fill(255);
			window.rect(x, this.distanceFromTop, this.digitWidth,
					this.digitHeight);
			window.fill(0);
			window.rect(x, this.distanceFromTop + this.lineWidth,
					2 * this.lineWidth, this.digitHeight - this.lineWidth);
			break;
		case (8):
			window.fill(255);
			window.rect(x, this.distanceFromTop, this.digitWidth,
					this.digitHeight);
			window.fill(0);
			window.rect(x + this.lineWidth, this.distanceFromTop
					+ this.lineWidth, this.lineWidth, this.lineWidth);
			window.rect(x + this.lineWidth, this.distanceFromTop + 3
					* this.lineWidth, this.lineWidth, this.lineWidth);
			break;
		case (9):
			window.fill(255);
			window.rect(x, this.distanceFromTop, this.digitWidth,
					this.digitHeight);
			window.fill(0);
			window.rect(x + this.lineWidth, this.distanceFromTop
					+ this.lineWidth, this.lineWidth, this.lineWidth);
			window.rect(x, this.distanceFromTop + 3 * this.lineWidth,
					2 * this.lineWidth, this.lineWidth);
			break;
		}
	}
}
